package com.hoyo.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.common.PageInfo;
import com.hoyo.domain.Bullet;
import com.hoyo.domain.Comment;
import com.hoyo.mapper.BulletMapper;
import com.hoyo.mapper.CommentMapper;
import com.hoyo.mapper.MovieMapper;
import com.hoyo.utils.TokenUtils;

@Service
public class MovieServiceImpl implements MovieService{
	
	@Autowired
	private MovieMapper movieMapper;
	
	@Autowired
	private BulletMapper bulletMapper;
	
	@Autowired
	private CommentMapper commentMapper;
	
	@Override
	public String playMovie(int movieId) throws Exception{
		String url = movieMapper.findUrlById(movieId);
//		update the movie play amount
		movieMapper.updatePlayAmountByMovieIdAndPlusAmount(movieId, 1);
		return TokenUtils.generateDownloadToken(url);
	}

	@Override
	public List<Bullet> findBulletsByMovieIdOnMoviePlayed(int movieId) throws Exception {
		return bulletMapper.findBulletsByMovieId(movieId);
	}

	@Override
	public PageInfo<Comment> pageFindCommentsByMovieId(int movieId, int pageNum) throws Exception {
		List<Comment> comments = commentMapper.findCommentsByMovieIdOrderByCreateTimeDescAndLimitBy(movieId, (pageNum-1)*10, 10);
		for (Comment comment : comments) {
			List<Comment> replyComments = commentMapper.findReplyCommentsByCommentIdOrderByLikPointsDescAndLimitBy(comment.getId(), 0, 3);
			int total = replyComments.size()>0?replyComments.get(0).getTotal():0;
			comment.setPageInfo(new PageInfo<>(pageNum, 3, total, replyComments));
		}
		int total = comments.size()>0?comments.get(0).getTotal():0;
		return new PageInfo<>(pageNum, 10, total, comments);
	}

	@Override
	public PageInfo<Comment> pageFindTopCommentsByMovieId(int movieId, int pageNum) throws Exception {
		List<Comment> topicComments = commentMapper.findCommentsByMovieIdOrderByLikePosintsDescLimitBy(movieId, (pageNum-1)*10, 10);
		for (Comment comment : topicComments) {
			List<Comment> replyComments = commentMapper.findReplyCommentsByCommentIdOrderByLikPointsDescAndLimitBy(comment.getId(), (pageNum-1)*3, 3);
			comment.setPageInfo(new PageInfo<>(1, 3, replyComments.size()>0?replyComments.get(0).getTotal():0, replyComments));
		}
		int total = topicComments.size()>0?topicComments.get(0).getTotal():0;
		return new PageInfo<>(pageNum, 10, total, topicComments);
	}

	@Override
	public JSONObject commentsOnMoviePlaying(int movieId) throws Exception {
		
		JSONObject obj = new JSONObject();
		List<Comment> topicComments = commentMapper.findCommentsByMovieIdOrderByLikePosintsDescLimitBy(movieId, 0, 3);
		for (Comment comment : topicComments) {
			List<Comment> replyComments = commentMapper.findReplyCommentsByCommentIdOrderByLikPointsDescAndLimitBy(comment.getId(), 0, 3);
			comment.setPageInfo(new PageInfo<>(1, 3, replyComments.size()>0?replyComments.get(0).getTotal():0, replyComments));
		}
		obj.put("topComments", topicComments);
		List<Comment> comments = commentMapper.findCommentsByMovieIdOrderByCreateTimeDescAndLimitBy(movieId, 0, 10);
		for (Comment comment : comments) {
			List<Comment> replyComments = commentMapper.findReplyCommentsByCommentIdOrderByLikPointsDescAndLimitBy(comment.getId(), 0, 3);
			comment.setPageInfo(new PageInfo<>(1, 3, replyComments.size()>0?replyComments.get(0).getTotal():0, replyComments));
		}
		int total = comments.size()>0?comments.get(0).getTotal():0;
		obj.put("commentPage", new PageInfo<>(1, 10, total, comments));
		
		return obj;
	}

	@Override
	public String translateId2url(int id) throws Exception{
		String url = movieMapper.findUrlById(id);
		return TokenUtils.generateDownloadToken(url);
	}
}
